home *** CD-ROM | disk | FTP | other *** search
/ PC-SIG: World of Education / PC-SiG's World of Education.iso / run / 2602 / stordata.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-26  |  3.1 KB  |  89 lines

  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include "datarec.h"
  4. #define MAX_DR_ENTRIES 63   /* nbr data pairs (x,f(x)) per data record */
  5.  
  6.  
  7. /*--------------------------------------------------*/
  8. /* store_data(flush,ofp,x,fx) - creates DAN compatible data files.  Caller
  9.             must have previously set file name in 'output_file' data
  10.             structure and reset the file open flg in that structure.
  11.         enter:    flush=0 => put data pt in output bfr & write data rec if full
  12.                     =1  => flush final bfr, close file, and free bfrs
  13.                     =2    => store last data pt and then flush, etc.
  14.                 ofp    = ptr to 'output_file' structure for data file
  15.                 x    = independent variable value
  16.                 fx    = dependent variable value
  17. */
  18.  
  19. void store_data(flush,ofp,x,fx)
  20.     int    flush;                    /* 0=normal call, 1=final call */
  21.     OF  *ofp;                    /* output file ptr */
  22.     double x,fx;                /* data values for cur pt */
  23. {
  24.     char msg[80];                /* err msg assembly bfr */
  25.     int  cnt;                    /* nbr data pts in output bfr */
  26.     DR   *bfp;                    /* output bfr ptr */
  27.  
  28.  
  29.             
  30.     if ((*ofp).open==0) {    /* have we opened the output file? */
  31.                             /* no - allocate space for output bfr */
  32.         bfp = (*ofp).bfr = (DR *)malloc(sizeof(DR));
  33.                             /* exit if no space available - try again nxt time */
  34.         if (bfp==NULL) return;        /* means may miss some data pts */
  35. sd10:
  36.         (*ofp).fid = fopen((*ofp).name,"w+b");
  37.         if ((*ofp).fid == NULL) {    /* was open OK? */
  38.             sprintf(msg,"Unable to open output file: %s\nRetry? (y/n) ",(*ofp).name);
  39.             if ( toupper(prompt(msg))=='Y') goto sd10;
  40.             (*ofp).open = 2;    /* indic file should be ignored */
  41.             return;
  42.             }
  43.         (*ofp).open = 1;        /* indic successful open */
  44.         (*bfp).dr_type = 7;        /* set data record type */
  45.         (*bfp).dr_pts = 0;        /* no data pts yet */
  46.         (*bfp).dr_rec_num = 1;    /* record number 1 */
  47.         }
  48.     if ((*ofp).open > 1) return;/* exit immediately if should ignore file */
  49.     bfp = (*ofp).bfr;            /* set ptr to output bfr */
  50.     cnt = (*bfp).dr_pts;        /* nbr data pts stored in bfr */
  51.     if (flush != 1) {            /* put data pt in bfr if not 'flush only' call */
  52.         (*bfp).dp[cnt].x = x;        /* put data pt in output bfr */
  53.         (*bfp).dp[cnt].fx = fx;
  54.         (*bfp).dr_pts = ++cnt;        /* incr nbr pts in bfr */
  55.         }
  56.     if (cnt >= MAX_DR_ENTRIES || flush) {    /* full? or last call? */
  57.                                 /* yes - output data record */
  58. sd20:    cnt = fwrite(bfp,sizeof(DR),1,(*ofp).fid);
  59.         if (cnt != 1) {            /* had write error? */
  60.             sprintf(msg,"Write error for output file: %s\nRetry? (y/n) ",(*ofp).name);
  61.             clearerr((*ofp).fid);/* clear file error status */
  62.             if ( toupper(prompt(msg))=='Y') goto sd20;
  63.             flush = 1;            /* force close now */
  64.             (*ofp).open = 3;    /* indic file should be ignored */
  65.             }
  66.         (*bfp).dr_pts = 0;        /* reset to empty */
  67.         if (flush) {
  68.             fclose((*ofp).fid);    /* close file on final call */
  69.             free((*ofp).bfr);    /* free the data bfr */
  70.             free(ofp);            /* free the output file structure */
  71.             }
  72.         }
  73.     return;
  74. }
  75.  
  76.  
  77.  
  78. /*--------------------------------------------------*/
  79. /* prompt(msg) - output message to user and wait for response.
  80.             Returns response (as an integer).
  81. */
  82.  
  83. prompt(msg)
  84.     char *msg;            /* msg to output */
  85. {
  86.     printf("%s",msg);
  87.     return getch();        /* return user's response to prompt msg */
  88. }
  89.